import java.rmi.RemoteException;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.List;


public class MagazineImpl extends UnicastRemoteObject implements Magazine {
	private static final long serialVersionUID = -3050147718329410057L;
	private static final int MAX_NEWS = 10;
	
    private SyncArrayList<News> newsList;
	
	
	public MagazineImpl() throws RemoteException {
		super();
		
		newsList = new SyncArrayList<News>();
	}
	
	public MagazineImpl(RMIClientSocketFactory clientFactory,
			RMIServerSocketFactory serverFactory) throws RemoteException
	{
		super(0,clientFactory,serverFactory);
		newsList = new SyncArrayList<News>();
	}

	@Override
	public synchronized List<News> getNews() {
		return newsList.toList();
	}

	@Override
	public synchronized void addNews(News n) {
		Date date = new Date();
		n.setCreationDate(date);
		newsList.add(0, n);
		if(newsList.size() == MAX_NEWS+1)
			newsList.remove(MAX_NEWS);
	}

}
